package com.salemanagement.service.impl;

import com.salemanagement.mapper.FrontUserMapper;
import com.salemanagement.service.FrontUserService;
import com.salemanagement.vo.RechargeVo;
import com.salemanagement.vo.RegisterUserVo;
import com.three.core.entity.FrontUser;
import com.three.core.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class FrontUserServiceImpl implements FrontUserService {
    @Autowired
    private FrontUserMapper frontUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${weixin.appid}")
    private String appId;
    @Value("${weixin.mch_id}")
    private String mchId;
    @Value("${weixin.api_key}")
    private String apiKey;
    @Override
    public R selectAllFontUser() {
        List<FrontUser> frontUsers = frontUserMapper.selectList(null);
        if (frontUsers != null){
            return new R<>(200, "查询成功", frontUsers);
        }else {
            return new R<>(500, "查询失败", null);
        }
    }
    @Override
    public R selectByPhone(String phone) {
        FrontUser frontUser = frontUserMapper.selectByPhone(phone);
        if (frontUser == null) {
            return new R<>(200, "用户还未注册", null);
        }else {
            return new R<>(500, "用户已注册", null);
        }
    }

    @Override
    public R sendSms(String phone) {
        Random random = new Random();
        int i = random.nextInt(999999);
        String code = String.format("%06d", i);
        redisTemplate.opsForValue().set(phone, code);
        redisTemplate.expire(phone, 1, TimeUnit.MINUTES);
        return new R<>(200, "验证码发送成功", code);
    }

    @Override
    public R checkSms(String phone, String code) {
        Object o = redisTemplate.opsForValue().get(phone);
        redisTemplate.delete(phone);
        if (o == null) {
            return new R<>(600, "验证码已过期", null);
        }else if (o.equals(code)){
            return new R<>(200, "验证码正确", null);
        }else {
            return new R<>(500, "验证码错误", null);
        }
    }

    @Override
    public R registerFrontUser(RegisterUserVo registerUserVo) {
        registerUserVo.setPassword(passwordEncoder.encode(registerUserVo.getPassword()));
        int insert = frontUserMapper.insertFrontUser(registerUserVo);
        if (insert > 0){
            return new R<>(200, "注册成功", null);
        }else {
            return new R<>(500, "注册失败", null);
        }
    }

    @Override
    public R updatePass(RegisterUserVo registerUserVo) {
        registerUserVo.setPassword(passwordEncoder.encode(registerUserVo.getPassword()));
        int i = frontUserMapper.updatePass(registerUserVo);
        if (i > 0){
            return new R<>(200, "修改成功", null);
        }else {
            return new R<>(500, "修改失败", null);
        }
    }
}
